# Python package
# Create and test a Python package on multiple Python versions.
# Add steps that analyze code, save the dist with the build record, publish to a PyPI-compatible index, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/python
trigger:
  tags:
    include:
      - '*'
  branches:
    include:
      - "master"
      - "refs/tags/*"
pr:
  - "master"

jobs:
  - job: special
    # how long to run the job before automatically cancelling
    timeoutInMinutes: "45"
    # how much time to give 'run always even if cancelled tasks' before stopping them
    cancelTimeoutInMinutes: "2"
    pool: "lit-rtx-3090"
    variables:
      DEVICES: $( python -c 'print("$(Agent.Name)".split("_")[-1])' )
    container:
      # image: "pytorchlightning/pytorch_lightning:base-cuda-py3.9-torch1.11"
      image: "pytorch/pytorch:1.11.0-cuda11.3-cudnn8-runtime"
      options: "--ipc=host --gpus=all -v /usr/bin/docker:/tmp/docker:ro"
    workspace:
      clean: all
    steps:

      - bash: |
          echo "##vso[task.setvariable variable=CUDA_VISIBLE_DEVICES]$(DEVICES)"
          echo "##vso[task.setvariable variable=CONTAINER_ID]$(head -1 /proc/self/cgroup|cut -d/ -f3)"
        displayName: 'Set environment variables'

      - bash: |
          echo $CUDA_VISIBLE_DEVICES
          lspci | egrep 'VGA|3D'
          whereis nvidia
          nvidia-smi
          python --version
          pip --version
          pip list
          df -kh /dev/shm
        displayName: 'Image info & NVIDIA'

      - bash: |
          python -c "import torch ; mgpu = torch.cuda.device_count() ; assert mgpu >= 2, f'GPU: {mgpu}'"
        displayName: 'Sanity check'

      - script: |
          python -m pip install "pip==22.2.1"
          pip --version
          pip install '.[image,test]' -r requirements/testing_image.txt -U
          pip list
        env:
          FREEZE_REQUIREMENTS: 1
        displayName: 'Install dependencies'

      - bash: |
          bash tests/special_tests.sh
        displayName: 'Testing: special'

      - bash: |
          python -m coverage report
          python -m coverage xml
          # python -m codecov --token=$(CODECOV_TOKEN) --commit=$(Build.SourceVersion) --flags=gpu,pytest --name="GPU-coverage" --env=linux,azure
          ls -l
        displayName: 'Statistics'
